home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / oop55.zip / BRICKS.PAS < prev    next >
Pascal/Delphi Source File  |  1989-05-02  |  3KB  |  151 lines

  1.  
  2. { Copyright (c) 1989 by Borland International, Inc. }
  3.  
  4. unit Bricks;
  5. { Turbo Pascal 5.5 object-oriented example.
  6.   See BREAKOUT.PAS.
  7.   This unit contains the Ball object and the object types that
  8.   end up as bricks on the screen.
  9. }
  10.  
  11. interface
  12.  
  13. uses Screen, Count;
  14.  
  15. type
  16.   Block = object(Location)
  17.     Color : Integer;
  18.     Width : Integer;
  19.     BChar : Char;
  20.     constructor Init(InitX, InitY, InitColor, InitWidth : Integer;
  21.                      InitChr : Char);
  22.     procedure Show; virtual;
  23.     procedure Hide; virtual;
  24.   end;
  25.  
  26.   Ball = object(Block)
  27.     XVel : Integer;
  28.     YVel : Integer;
  29.     constructor Init(InitX, InitY, InitXVel, InitYVel, InitColor : Integer);
  30.     function NextX : Integer;
  31.     function NextY : Integer;
  32.     procedure MoveX;
  33.     procedure MoveY;
  34.     procedure ReverseX;
  35.     procedure ReverseY;
  36.     procedure ChangeXVel(Delta : Integer);
  37.   end;
  38.  
  39.   Brick = object(Block)
  40.     Value : Integer;
  41.     constructor Init(InitX, InitY, InitColor, InitValue : Integer);
  42.     function GetValue : Integer;
  43.   end;
  44.  
  45. implementation
  46.  
  47. uses Crt;
  48.  
  49. constructor Block.Init(InitX, InitY, InitColor, InitWidth : Integer;
  50.                        InitChr : Char);
  51. begin
  52.   Location.Init(InitX, InitY);
  53.   Color := InitColor;
  54.   Width := InitWidth;
  55.   BChar := InitChr;
  56. end;
  57.  
  58. procedure Block.Show;
  59. var
  60.   Str : String[10];
  61. begin
  62.   FillChar(Str[1], Width, BChar);
  63.   Str[0] := Chr(Width);
  64.   Location.Show;
  65.   TextColor(Color);
  66.   GoToXY(X, Y);
  67.   Write(Str);
  68. end;
  69.  
  70. procedure Block.Hide;
  71. begin
  72.   Location.Hide;
  73.   GoToXY(X, Y);
  74.   Write('' : Width);
  75. end;
  76.  
  77. constructor Brick.Init(InitX, InitY, InitColor, InitValue : Integer);
  78. var
  79.   BlockChar : Char;
  80. begin
  81.   BlockChar := Chr($B2);
  82.   if (LastMode = Mono) and Odd(InitX + InitY) then
  83.         BlockChar := Chr($B0);
  84.   Block.Init(InitX, InitY, InitColor, 5, BlockChar);
  85.   Value := InitValue;
  86. end;
  87.  
  88. function Brick.GetValue : Integer;
  89. begin
  90.   GetValue := Value;
  91. end;
  92.  
  93. constructor Ball.Init(InitX, InitY, InitXVel, InitYVel, InitColor : Integer);
  94. begin
  95.   Block.Init(InitX, InitY, InitColor, 1, Chr(15));
  96.   XVel := InitXVel;
  97.   YVel := InitYVel;
  98. end;
  99.  
  100. function Ball.NextX : Integer;
  101. begin
  102.   NextX := X + XVel;
  103. end;
  104.  
  105. function Ball.NextY : Integer;
  106. begin
  107.   NextY := Y + YVel;
  108. end;
  109.  
  110. procedure Ball.MoveX;
  111. begin
  112.   Hide;
  113.   X := NextX;
  114.   Show;
  115. end;
  116.  
  117. procedure Ball.MoveY;
  118. begin
  119.   Hide;
  120.   Y := NextY;
  121.   Show;
  122. end;
  123.  
  124. procedure Ball.ReverseX;
  125. begin
  126.   XVel := -XVel;
  127. end;
  128.  
  129. procedure Ball.ReverseY;
  130. begin
  131.   YVel := -YVel;
  132. end;
  133.  
  134. { This procedure introduces the variations in horizontal velocity for
  135.   the ball.  Horizontal velocity ranges from -2 to 2.  If you hit the
  136.   ball with the edge of the paddle, you'll get a large change in
  137.   horizontal velocity. }
  138.  
  139. procedure Ball.ChangeXVel(Delta : Integer);
  140. begin
  141.   Inc(XVel, Delta);
  142.   if XVel < -2 then
  143.     XVel := -2
  144.   else if XVel > 2 then
  145.     XVel := 2
  146.   else if XVel = 0 then
  147.     XVel := Integer(Random(2))*2 - 1;
  148. end;
  149.  
  150. end.
  151.